package com.doushu.dispatch.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.doushu.dispatch.common.dto.ResultDto;
import com.doushu.dispatch.common.dto.ResultDtoFactory;
import com.doushu.dispatch.common.property.EdoushuProperty;
import com.doushu.dispatch.common.utils.HttpUtil;
import com.doushu.dispatch.mapper.ItemFromMapper;
import com.doushu.dispatch.mapper.ItemMapper;
import com.doushu.dispatch.mapper.KeywordAnalysisMapper;
import com.doushu.dispatch.mapper.KeywordZTCMapper;
import com.doushu.dispatch.service.DataVerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class DataVerServiceImpl implements DataVerService {

    @SuppressWarnings("all")
    @Autowired
    private ItemMapper itemMapper;

    @SuppressWarnings("all")
    @Autowired
    private KeywordZTCMapper keywordZTCMapper;

    @SuppressWarnings("all")
    @Autowired
    private ItemFromMapper itemFromMapper;

    @SuppressWarnings("all")
    @Autowired
    private KeywordAnalysisMapper keywordAnalysisMapper;

    @Autowired
    private EdoushuProperty edoushuProperty;

    @Override
    public ResultDto itemsEffectDetailUpVer(Long shopId) {
        Boolean result = itemMapper.existsBySid(shopId);
        if(result!=null&&result){
            return ResultDtoFactory.toAck("success",true);
        }
        return ResultDtoFactory.toNack("failure",false);
    }

    @Override
    public ResultDto listFlowFromUpVer(Long shopId, String datatime, Long itemId) {
        Boolean result = itemFromMapper.existsBySidAndDatatimeAndIid(shopId,datatime,itemId);
        if(result!=null&&result){
            List<JSONObject> list = new ArrayList<JSONObject>();
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("itemid",itemId);
            list.add(jsonObject);
            JSONObject params = new JSONObject();
            params.put("itemId",list);
            params.put("dataDate",datatime);
            HttpUtil.jsonClient(edoushuProperty.getPrefix() + edoushuProperty.getOperateN(),params);
            return ResultDtoFactory.toAck("success",true);
        }
        return ResultDtoFactory.toNack("failure",false);
    }

    @Override
    public ResultDto carDataUpVer(Long shopId, String datatime) {
        List<Long> items = keywordZTCMapper.findItemsBySidAndDatatime(shopId,datatime);
        if(items.size()>0) {
            List<JSONObject> distinctList = new ArrayList<JSONObject>();
            List<Long> cList = new ArrayList<Long>();
            for (Long item: items) {
                if (!cList.contains(item)) {
                    JSONObject jsonObject = new JSONObject();
                    jsonObject.put("itemid",item);
                    distinctList.add(jsonObject);
                    cList.add(item);
                }
            }
            JSONObject params = new JSONObject();
            params.put("dataDate",datatime);
            params.put("itemId",distinctList);
            HttpUtil.jsonClient(edoushuProperty.getPrefix() + edoushuProperty.getOperateZ(),params);
            return ResultDtoFactory.toAck("success", true);
        }
        return ResultDtoFactory.toNack("failure",false);
    }

    @Override
    public ResultDto itemKeywordsUpVer(Long shopId, String datatime, Long itemId) {
        Boolean result =  keywordAnalysisMapper.existsBySidAndDatatimeAndIid(shopId,datatime,itemId);
        if(result!=null&&result){
            return ResultDtoFactory.toAck("success",true);
        }
        return ResultDtoFactory.toNack("failure",false);
    }
}
